package com.cuizhang.demo;

import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ll.core.action.QueryAction;
import com.ll.core.magzine.model.bean.Period;
import com.ll.core.util.JSONTool;
import com.ll.core.view.util.ViewUtil;

public class HomePageActivity extends Activity {
	private final static int DIALOG_LOADING = 1001;
	private final static int DIALOG_LOADING_ERROR = 1002;
	
	private List<Period> periods;
	private ProgressDialog dialog;
	private int[] ids = new int[] { R.id.hq1, R.id.hq2, R.id.hq3, R.id.hq4, R.id.hq5, R.id.hq6, R.id.hq7, R.id.hq8, R.id.hq9, R.id.hq10, R.id.hq11,
			R.id.hq12 };


	public void loadPeriods(){
		String year = "2011";
		try {
			this.periods = (List<Period>)QueryAction.getInstance().queryPeriodsByYear(year);
		} catch (Exception e) {
			this.onCreateDialog(DIALOG_LOADING_ERROR);
			e.printStackTrace();
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.home_page_main);

		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_text_icon);
		((TextView) findViewById(R.id.title)).setText("开发区报道");
		//加载本年的期刊
		loadPeriods();
		for (int i = 0; i < ids.length; i++) {
			((ImageView)(findViewById(ids[i]).findViewById(R.id.image))).setVisibility(View.INVISIBLE);
		}
		
		for (int i = 0; i < ids.length; i++) {
			
			if(periods.size() < i+1){
				break;
			}
			final int ii = i;
			final View v = findViewById(ids[i]);
			Period period = periods.get(i);
			v.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					Intent i = new Intent(HomePageActivity.this, StageActivity.class);
//					i.putExtra("period", dates);
//					i.putExtra("stageByYears", stageByYears);
//					i.putExtra("stageByAlls", stageByAlls);
//					i.putExtra("imageIds", imageIds);
//					i.putExtra("index", ii);
					i.putExtra("period", JSONTool.ObjectToJSON(periods.get(ii)));
					startActivity(i);
				}
			});
			
			
			ImageView imageView = (ImageView) v.findViewById(R.id.image);
			TextView dateView = (TextView) v.findViewById(R.id.date);
			TextView stageView = (TextView) v.findViewById(R.id.stage);
			imageView.setVisibility(View.VISIBLE);
			imageView.setImageBitmap(ViewUtil.loadImage(period.getCover_id()));
			dateView.setText(period.getPublish_timeFormat());
			stageView.setText("总第"+period.getPeriod()+"期");
		}

		ImageView btnLeft = (ImageView) findViewById(R.id.search_stock_left);
		ImageView btnRight = (ImageView) findViewById(R.id.search_stock_right);
		btnLeft.setVisibility(View.INVISIBLE);
		btnRight.setVisibility(View.INVISIBLE);

		Button lastPeriodButton = (Button) findViewById(R.id.last_period);
		lastPeriodButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent i = new Intent(HomePageActivity.this, LastPeriodActivity.class);
				startActivity(i);

			}
		});

	}
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_LOADING:
			dialog = new ProgressDialog(this);
			dialog.setMessage("数据加载中......");
			dialog.setIndeterminate(true);
			return dialog;
		case DIALOG_LOADING_ERROR:
			dialog = new ProgressDialog(this);
			dialog.setMessage("数据加载失败......");
			dialog.setIndeterminate(true);
			return dialog;
	
			

		}
		return super.onCreateDialog(id);
	}
}